<?php

class UploadParser {

    private $raw = "";
    private $name = "";

    function __construct($id) {
        ini_set("upload_max_filesize", "2048M");
        ini_set("post_max_size", "2048M");

        $this->raw = $_POST[$id][1];
        $this->name = $_POST[$id][0];
    }

    function getExt() {
        $ext = explode("/", $this->getMime());
        $ext = $ext[1];
        return $ext;
    }

    function getMime() {
        $tmp = explode(";", $this->raw);
        $tmp = $tmp[0];
        $mime = explode(":", $tmp);
        $mime = $mime[1];
        return $mime;
    }

    function getBase64() {
        $tmp = explode(";", $this->raw);
        $tmp = $tmp[1];
        $base = explode(",", $tmp);
        $base = $base[1];
        return $base;
    }

    function getData() {
        return base64_decode($this->getBase64());
    }

    function getName() {
        return $this->name;
    }

    function getRaw() {
        return $this->raw;
    }

    function save($fileName = "") {
        if ($fileName == "") {
            $fileName = $this->getName();
        }
        file_put_contents($fileName, $this->getData());
    }

}

class Element {

    public $id = "";
    private $pureID = "";

    function __construct($id = "") {
        $this->id = "#" . $id;
        $this->pureID = $id;
    }

    public function remove() {
        echo "$('$this->id').remove();";
    }

    public function create($tag, $atributes, $content = NULL) {
        $atrs = "";
        $html = "";
        foreach ($atributes as $key => $value) {
            $atrs .= "$key = '$value' ";
        }
        if ($content == NULL) {
            $html = "<{$tag} {$atrs} />";
        } else {
            $html = "<{$tag} {$atrs} >$content</{$tag}>";
        }
        $this->add($html);
    }

    public function fadeIn($tempo = 256) {
        fadeIn($this->id, $tempo);
    }

    public function fadeOut($tempo = 256) {
        fadeOut($this->id, $tempo);
    }

    public function getValue() {
        return (addslashes($_POST[$this->pureID]));
    }

    public function setValid($value) {
        setValid($this->id, $value);
    }

    public function setFocus() {
        setfocus($this->id);
    }

    public function setValue($value) {
        setValue($this->id, $value);
    }

    public function enable() {
        enable($this->id);
    }

    public function disable() {
        disable($this->id);
    }

    public function show() {
        show($this->id);
    }

    public function hide() {
        hide($this->id);
    }

    public function add($value) {
        $value = replace($value, "\n", "");
        $value = addslashes($value);
        echo "$('$this->id').append(\"$value\");valueUpdate(\"" . conf::$pkjHome . "\");";
    }

    public function html($value) {
        $value = replace($value, "\n", "");
        $value = addslashes($value);
        echo "$('$this->id').html(\"$value\");valueUpdate(\"" . conf::$pkjHome . "\");";
    }

    function atribute($key, $value) {
        $value = replace($value, "\n", "");
        $value = addslashes($value);
        echo "$('$this->id').attr(\"$key\",\"$value\");valueUpdate(\"" . conf::$pkjHome . "\");";
    }

}

function getElementById($id) {
    return new Element($id);
}

function redirect($url) {
    echo "location.href='$url';";
}

function setTimeout($function, $time) {
    $function_name = explode("(", $function);
    $function_name = $function_name[0];
    $function_name = trim($function_name);
    $vName = $function_name;
    echo "$vName = setTimeout(function(){ bindCall('','$function'); },$time);";
}

function setInterval($function, $time) {
    $function_name = explode("(", $function);
    $function_name = $function_name[0];
    $function_name = trim($function_name);
    $vName = $function_name;
    echo "$vName = setInterval(function(){ bindCall('','$function'); },$time);";
}

function clearInterval($name) {
    echo "clearInterval($name);";
}

function clearTimeout($name) {
    echo "clearTimeout($name);";
}

function getHtml() {
    return getResponse();
}

function get_html() {
    return getResponse();
}

function getEcho() {
    return getResponse();
}

function get_echo() {
    return getResponse();
}

function getResponse() {
    $html = ob_get_contents();
    ob_clean();
    $html = str_replace(array("\n", "\r"), ' ', $html);
    return $html;
}

function get_response() {
    return getResponse();
}

function set_valid($id, $trueorfalse) {
    return setValid($id, $trueorfalse);
}

function setValid($id, $trueorfalse) {
    if ($trueorfalse):
        echo "$(\"{$id}\").css(\"boxShadow\",\"0 0 7px #6DB9FF\");";
    elseif ($trueorfalse == NULL):
        echo "$(\"{$id}\").css(\"boxShadow\",\"0 0 0 white\");";
    else:
        echo "$(\"{$id}\").css(\"boxShadow\",\"0 0 7px red\");";
    endif;
}

function set_focus($id) {
    setFocus($id);
}

function setFocus($id) {
    echo "$(\"{$id}\").focus();";
}

function set_value($id, $value) {
    setValue($id, $value);
}

function setValue($id, $value) {
    if (substring($id, 1, 2) == "rd" || substring($id, 1, 3) == "chk"):
        if (castBoolean($value)):
            setCheck($id);
        else:
            setUncheck($id);
        endif;
    else:
        echo "$(\"{$id}\").val(\"{$value}\");";
    endif;
}

function enable($id) {
    echo "$(\"{$id}\").removeAttr(\"disabled\");";
}

function disable($id) {
    echo "$(\"{$id}\").attr(\"disabled\",\"true\");";
}

function fadeIn($id, $tempo) {
    echo "$(\"{$id}\").fadeIn($tempo);";
}

function fadeOut($id, $tempo) {
    echo "$(\"{$id}\").fadeOut($tempo);";
}

function hide($id) {
    echo "$(\"{$id}\").hide(256);";
}

function show($id) {
    echo "$(\"{$id}\").show(256);";
}

function alert($msg) {
    $msg = base64_encode($msg);
    echo "alert(utf8_decode(base64_decode('{$msg}')));";
}

function html($conteudo, $elemento = "body") {
    $conteudo = base64_encode($conteudo);
    echo "$(\"{$elemento}\").html(utf8_decode(base64_decode('{$conteudo}')));sisBindRequest();";
}

function write($conteudo, $elemento = "body") {
    $conteudo = base64_encode($conteudo);
    echo "$(\"{$elemento}\").append(utf8_decode(base64_decode('{$conteudo}')));sisBindRequest();";
}

function writeln($conteudo, $elemento = "body") {
    $conteudo = base64_encode($conteudo);
    echo "$(\"{$elemento}\").append(utf8_decode(base64_decode('{$conteudo}')));sisBindRequest();";
}

function append($conteudo, $elemento = "body") {
    write($conteudo, $elemento);
}

function set_check($id) {
    setCheck($id);
}

function setCheck($id) {
    echo "$(\"{$id}\").attr(\"checked\",true);";
}

function set_unchec($id) {
    setUncheck($id);
}

function setUncheck($id) {
    echo "$(\"{$id}\").attr(\"checked\",false);";
    echo "$(\"{$id}\").removeAttr(\"checked\");";
}

function notice($mensagem, $nivel = "alert") {
    $mensagem = base64_encode(nl2br($mensagem));
    echo "notice(utf8_decode(base64_decode('{$mensagem}')),'{$nivel}');";
}

function clear_notice($tempo) {
    clearNotice($tempo);
}

function clearNotice($tempo) {
    echo "setTimeout(function(){ $('.notice').hide(256,function(){ $(this).remove() }); },{$tempo});";
}

function console($mensagem) {
    $mensagem = base64_encode($mensagem);
    echo "console.log(utf8_decode(base64_decode('{$mensagem}')));";
}

function popup($mensagem) {
	echo "$('#pkjpopup').remove();";
    echo "$('body').append(utf8_decode(base64_decode('" . base64_encode("<div id='pkjpopup' ><a class=\"b-close\">x<a/> $mensagem</div>") . "')));";
    echo "$('#pkjpopup').bPopup();";
}

function bindUpdate() {
    echo "valueUpdate(\"" . conf::$pkjHome . "\");sisBindRequest();";
}

?>
